<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Base extends Controller
{
    public $account;

    public function _initialize()
    {
        $isLogin = $this->is_login();
        if (!$isLogin) {
            $this->redirect(url('login/index'));
        }
        //根据用户拥有的权限分配
        if ($role_id = $this->getLoginUser()->mg_id != 1) {
            //普通管理员
            //获取ps_ca
            $ps_ca = model('role')->field('ps_ca')->find(['role_id', $role_id])->toArray();
            $nowCA = \request()->controller() . '-' . \request()->action();
            if (strpos($ps_ca['ps_ca'], $nowCA) === false) {
                exit('快滚');
            }
        }
    }

    //判断是否登录
    public function is_login()
    {
        $user = $this->getLoginUser();
        if ($user && $user->mg_id) {
            return true;
        }
        return false;
    }

    //获取session值
    public function getLoginUser()
    {
        if (!$this->account) {
            //判断用户是否登录
            return $this->account = session('manager', '', 'admin');
        }
        return $this->account;
    }
    //更新状态
    //修改状态
    public function status()
    {
        $data = input('get.');
        //进行更新操作
        if (empty($data['id'])) {
            $this->error('id不正确');
        }
        $model = \request()->controller();
        $res = model($model)->save(['status' => $data['status']], ['id' => $data['id']]);
        if (!$res) {
            $this->error('更新失败');
        } else {
            $this->success('更新成功');
        }
    }
}
